package cn.edu.ren.service.imp;

import cn.edu.ren.dao.ItemDao;
import cn.edu.ren.entity.Item;
import cn.edu.ren.service.ItemService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * @PackageName: cn.edu.ren.service
 * @ClassName: ItemServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2021/7/8  0:07
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemDao itemDao;

    // 排序
    @Override
    public List<Item> searchAndSort() {
        List<Item> items = new ArrayList<>();
        // 构建条件查询对象
        // 构建查询条件
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询
        nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("category", "手机"));
        // 排序
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
        // 分页查询
        Page<Item> search = this.itemDao.search(nativeSearchQueryBuilder.build());

        // 获取迭代对象
        Iterator<Item> iterator = search.iterator();
        // 循环遍历迭代
        while (iterator.hasNext()) {
            // 获取item对象
            Item item = iterator.next();
            // 存入集合
            items.add(item);
        }

        return items;
    }

    // rangeQuery   范围匹配
    @Override
    public List<Item> rangeQuery() {
        List<Item> items = new ArrayList<>();
        // 构建条件查询对象
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.rangeQuery("price").from(2000).to(2500));
        // 分页查询
        Page<Item> search = itemDao.search(nativeSearchQueryBuilder.build().getQuery(), PageRequest.of(0, 3));
        // 获取迭代对象
        Iterator<Item> iterator = search.iterator();
        // 循环遍历迭代
        while (iterator.hasNext()) {
            // 获取item对象
            Item item = iterator.next();
            // 存入集合
            items.add(item);
        }

        return items;
    }

    // booleanQuery  布尔查询查询
    @Override
    public List<Item> booleanQuery() {
        List<Item> items = new ArrayList<>();
        // 构建条件查询对象
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
                .withQuery(
                        QueryBuilders.boolQuery()
                                .must(QueryBuilders.termQuery("title", "小米"))
                                .must(QueryBuilders.termQuery("brand", "手机"))
                );
        // 分页查询
        Page<Item> search = itemDao.search(nativeSearchQueryBuilder.build().getQuery(), PageRequest.of(0, 3));
        // 获取迭代对象
        Iterator<Item> iterator = search.iterator();
        // 循环遍历迭代
        while (iterator.hasNext()) {
            // 获取item对象
            Item item = iterator.next();
            // 存入集合
            items.add(item);
        }

        return items;
    }

    // fuzzyQuery  通配符匹配
    @Override
    public List<Item> fuzzyQuery() {
        List<Item> items = new ArrayList<>();
        // 构建条件查询并添加查询条件
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.fuzzyQuery("title", "华为"));
        // 分页查询
        Page<Item> search = itemDao.search(nativeSearchQueryBuilder.build().getQuery(), PageRequest.of(0, 3));
        // 获取迭代对象
        Iterator<Item> iterator = search.iterator();
        // 循环遍历迭代
        while (iterator.hasNext()) {
            // 获取item对象
            Item item = iterator.next();
            // 存入集合
            items.add(item);
        }

        return items;
    }

    // 词条匹配，不分词
    @Override
    public List<Item> termQuery() {
        List<Item> items = new ArrayList<>();
        // 构建条件查询并添加查询条件
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.termQuery("brand", "手机"));
        // 查询 自动分页 ,默认查找第一页的10条数据，可以指定分页信息
        Page<Item> search = itemDao.search(nativeSearchQueryBuilder.build().getQuery(), PageRequest.of(0, 5));
        // 获取迭代对象
        Iterator<Item> iterator = search.iterator();
        // 循环遍历迭代
        while (iterator.hasNext()) {
            // 获取item对象
            Item item = iterator.next();
            // 存入集合
            items.add(item);
        }

        return items;
    }

    // match query:词条匹配，先分词然后在调用termQuery进行匹配
    @Override
    public List<Item> matchQuery() {
        List<Item> items = new ArrayList<>();
        // 构建条件查询
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询  QueryBuilders.matchQuery(查询域, 查询词)
        nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("brand", "手机"));
        // 搜索，获取结果
        Page<Item> page = this.itemDao.search(nativeSearchQueryBuilder.build());
        // 获取搜索总条数
        System.err.println(page.getTotalElements());
        // 获取总页数
        System.err.println(page.getTotalPages());
        // 获取内容
        System.err.println(page.getContent());
        // 获取迭代对象
        Iterator<Item> iterator = page.iterator();
        // 循环遍历迭代
        while (iterator.hasNext()) {
            // 获取item对象
            Item item = iterator.next();
            // 存入集合
            items.add(item);
        }

        return items;
    }

    @Override
    public List<Item> findAll(Sort sort) {
        List<Item> itemList = new ArrayList<>();
        Iterable<Item> iterable = itemDao.findAll(sort);
        Iterator<Item> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            Item next = iterator.next();
            itemList.add(next);
        }
        return itemList;
    }

    @Override
    public List<Item> findAll(Pageable pageable) {
        Page<Item> page = itemDao.findAll(pageable);
        List<Item> itemList = page.getContent();
        return itemList;
    }

    @Override
    public void save(Item item) {
        itemDao.save(item);
    }

    @Override
    public void batchSave(List<Item> itemList) {
        itemDao.saveAll(itemList);
    }

    @Override
    public List<Item> findAll() {
        List<Item> itemList = new ArrayList<>();
        // 查询所有
        Iterable<Item> items = itemDao.findAll();
        // 获取迭代对象
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()){
            Item next = iterator.next();
            // 存入集合
            itemList.add(next);
        }
        return itemList;
    }

    @Override
    public Item findById(Long id) {
        Optional<Item> optionalItem = itemDao.findById(id);
        Item item = optionalItem.get();
        return item;
    }

    @Override
    public List<Item> findByIds(List<Long> ids) {
        List<Item> itemList = new ArrayList<>();
        Iterable<Item> iterable = itemDao.findAllById(ids);
        Iterator<Item> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            Item next = iterator.next();
            itemList.add(next);
        }
        return itemList;
    }
}
